package net.osc.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyRunner {
	private static final Log LOG = LogFactory.getLog(JettyRunner.class);

	private String contextPath = "/";
	private String resourceBasePath = "src/main/webapp";
	private int port = 8080;

	public JettyRunner() {
	}

	public JettyRunner(String contextPath, int port) {
		this.contextPath = contextPath;
		this.port = port;
	}

	public void run() {
		try {
			Server server = new Server();
			SelectChannelConnector connector = new SelectChannelConnector();
			connector.setPort(port);
			server.addConnector(connector);

			WebAppContext context = new WebAppContext();
			context.setResourceBase(resourceBasePath);
			context.setInitParameter("useFileMappedBuffer", "false");
			context.setContextPath(contextPath);
			context.setParentLoaderPriority(true);

			server.setHandler(context);
			server.start();
			LOG.info("- -  J E T T Y  S T A R T  - -");
			server.join();
		}
		catch (Exception e) {
			LOG.error(e, e);
		}
	}

	public String getContextPath() {
		return contextPath;
	}

	public JettyRunner setContextPath(String contextPath) {
		this.contextPath = contextPath;
		return this;
	}

	public String getResourceBasePath() {
		return resourceBasePath;
	}

	public JettyRunner setResourceBasePath(String resourceBasePath) {
		this.resourceBasePath = resourceBasePath;
		return this;
	}

	public int getPort() {
		return port;
	}

	public JettyRunner setPort(int port) {
		this.port = port;
		return this;
	}
	
	

}
